package com.along.htybrowser.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;

import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;

/**
 * 文件操作
 *
 * @author wen66to88@163.com
 * @date 2021/7/6 - 14:37
 */
public class FileUtil {
    public static File dir;

    public static void initFile(String dirPath) {
        //新建文件夹存放数据库文件
        String path = dirPath + File.separator + "HTYBrowser";
        dir = new File(path);
        if (!dir.exists()) {
            dir.mkdirs();
        }
    }

    /**
     * 读取文件内容
     *
     * @param context
     * @param filename
     * @return
     */
    public static String ReadFile(Context context, String filename) {
        String s = "";
        FileInputStream fileInputStream;
        try {
            fileInputStream = context.openFileInput(filename);
            byte[] buffer = new byte[1024];
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            int len;
            while ((len = fileInputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, len);
            }
            s = new String(outputStream.toByteArray());
            fileInputStream.close();
            outputStream.close();
            return s;
        } catch (FileNotFoundException e) {
            AutoLog.e("文件未找到" + filename + " === " + e);
            String path = context.getFilesDir().getAbsolutePath() + "/blockrules";
            File file = new File(path);
            try {
                file.createNewFile();
            } catch (IOException e1) {
                AutoLog.e("创建文件失败：" + e1);
            }
            return s;
        } catch (IOException e) {
            AutoLog.e("读取文件失败：" + e);
            return s;
        }
    }

    /**
     * 写入内容
     *
     * @param content a
     * @return a
     */
    public static boolean addRule(Context context, String fileName, String content) {
        FileOutputStream outStream = null;
        try {
            outStream = context.openFileOutput(fileName, Context.MODE_APPEND);
            outStream.write((content + "\n").getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    public static String writeFile(String filename, String s) {
        File file = new File(FileUtil.dir, filename);
        try {
            BufferedWriter BW = new BufferedWriter(new FileWriter(file, false)); //false覆盖
            if (filename.endsWith(".csv")) {
                BW.write('\ufeff'); // Excel通过文件的BOM头来判断文件编码
                BW.flush();
            }
            BW.write(s);
            BW.flush();
        } catch (Exception e) {
            Log.e(Thread.currentThread().getStackTrace()[2] + "", e.toString());
            return null;
        }
        return filename;
    }

    public static boolean fileWrite(String fileName, String content) {
        File saveFile = new File(dir, fileName);
        FileOutputStream outStream = null;
        try {
            outStream = new FileOutputStream(saveFile);
            outStream.write(content.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                outStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return true;
    }
}
